var width = window.innerWidth || window.screen.width || document.body.clientWidth;
document.getElementsByTagName('html')[0].style.fontSize = width / 15 + 'px';
var ai = (function () {
  var server = {
    forum: 'https://wechatlt.jkbanlv.com',
    shop: 'https://wechatlt.jkbanlv.com/api',
    bpshop: 'http://integral.jkbanlv.com/integral',
    games: 'https://wechat.jkbanlv.com/api',
  };
  var token = {
    games: 'QBeXrkby',
  };
  var os = {
    ios: function () {
      return /(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)
    },
    android: function () {
      return /(Android)/i.test(navigator.userAgent)
    },
    wechat: function () {
      var ua = navigator.userAgent.toLowerCase();
      return ua.match(/MicroMessenger/i) == "micromessenger"
    }
  };
  var url = function () {
    if (os.wechat()) {
      return 'http://a.app.qq.com/o/simple.jsp?pkgname=com.jkbanlv.kanglv'
    } else if (os.android()) {
      return 'https://rans-7900.oss-cn-beijing.aliyuncs.com/android_version/%E5%BA%B7%E4%BE%A3.apk'
    } else if (os.ios()) {
      return 'https://itunes.apple.com/cn/app/wei-xin/id1386522774?mt=8'
    } else {
      return 'www.jkbanlv.com'
    }
  };
  var getQuery = function (query) {
    var reg = new RegExp("(^|&)" + query + "=([^&]*)(&|$)");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) {
      return unescape(r[2])
    } else {
      return null
    }
  };
  var convertImage = function (canvas) {
    var image = new Image();
    image.src = canvas.toDataURL("image/png");
    return image;
  };
  var dateInavlid = function (time) {
    if (time) {
      time = time.replace(/-/g, ':').replace(' ', ':');
      time = time.split(':');
      var newTime = new Date(time[0], (time[1] - 1), time[2], time[3], time[4], time[5]);
      return newTime
    }
  };
  var ajax = function (set, call) {
    set.query = set.query || { pcs: 3 };
    set.query.pcs = 3;
    // set.withToken ? set.query.token = token[set.withToken] : '';
    set.query.token = set.withToken || '';
    if(set.query.token){
      $.ajax({
        url: server[set.urlType] + set.url,
        type: set.type || 'GET',
        data: set.query,
        dataType: set.dataType || 'JSON',
        success: function (res) {
          // console.log(res)
          if (set.parse) {
            res = JSON.parse(res);
          }
          if (set.origin) {
            call(res)
          } else {
            res.msg ? call(res.msg) : call(res.data.msg)
          }
        },
        error: function (err) {
          console.log(err)
        }
      })
    }else{
      console.log('请携带 token')
    }
  };
  return {
    server: server,
    os: os,
    url: url,
    getQuery: getQuery,
    convertImage: convertImage,
    ajax: ajax
  }
})();